package com.momin.gmall.product.controller;

import java.util.Arrays;
import java.util.Collections;
import java.util.Map;

import com.momin.gmall.product.dao.BrandDao;
import com.momin.gmall.product.dao.CategoryDao;
import com.momin.gmall.product.entity.BrandEntity;
import com.momin.gmall.product.entity.CategoryEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import com.momin.gmall.product.entity.CategoryBrandRelationEntity;
import com.momin.gmall.product.service.CategoryBrandRelationService;
import com.momin.common.utils.PageUtils;
import com.momin.common.utils.R;



/**
 * 品牌分类关联
 *
 * @author momin
 * @email sunlightcs@gmail.com
 * @date 2021-03-15 17:27:12
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    @Autowired
    private CategoryDao categoryDao;

    @Autowired
    private BrandDao brandDao;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryBrandRelationService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @GetMapping("/catelog/list/{brandId}")
    public R brandCategoryList(@RequestParam Map<String, Object> params,@PathVariable("brandId")Long brandId){
//        PageUtils page = categoryBrandRelationService.queryPage(params);
        PageUtils page = categoryBrandRelationService.queryBrandCategoryPage(params, brandId);
        return R.ok().put("data", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){

        if(!CollectionUtils.isEmpty(categoryBrandRelationService.queryBrandCategoryByBandIdAndCatId(categoryBrandRelation.getBrandId(),categoryBrandRelation.getCatelogId()))){
            return R.error("关联失败，数据已存在!");
        }

        BrandEntity brandEntity = brandDao.selectById(categoryBrandRelation.getBrandId());

        if(brandEntity!=null){
            categoryBrandRelation.setBrandName(brandEntity.getName());
        }

        CategoryEntity categoryEntity = categoryDao.selectById(categoryBrandRelation.getCatelogId());
        if(categoryEntity!=null){
            categoryBrandRelation.setCatelogName(categoryEntity.getName());
        }
        categoryBrandRelationService.save(categoryBrandRelation);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }


}
